package com.tiza.xiaoyoubao.ui.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.EditInfo;

import org.simple.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

/**
 * Created by mapple on 17/4/4.
 */

public class ChooseAgeFragment extends BaseFragment {

    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_age)
    TextView tv_age;
    private String year;
    private int age;
    private TimePickerView pvCustomTime;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_age;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        initCustomTimePicker();
        pvCustomTime.show();

    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getNowYear() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(new Date());
    }


    private void initCustomTimePicker() {

        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerView.Builder(mActivity,
                                                  new TimePickerView.OnTimeSelectListener() {
                                                      @Override
                                                      public void onTimeSelect(
                                                              Date date, View v
                                                      ) {//选中事件回调
                                                          tv_date.setText(getTime(date));
                                                          year = tv_date.getText().toString();
                                                          year = year.substring(0, 4);
                                                          age = Integer.parseInt(
                                                                  getNowYear()) - Integer.parseInt(
                                                                  year);
                                                          tv_age.setText(age + "周岁");
                                                          EventBus.getDefault().post(new EditInfo(
                                                                  EditInfo.TYPE_TAOBAO_AGE,
                                                                  String.valueOf(age)
                                                          ), Constants.TAG_SET_TAOBAO_SUCCESS);
                                                          finish();
                                                      }
                                                  }
        ).setDate(selectedDate).setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomTime.returnData();
                        pvCustomTime.dismiss();
                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomTime.dismiss();
                        finish();
                    }
                });
            }
        }).setType(new boolean[]{true, true, true, false, false, false}).isCenterLabel(
                false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
         .setDividerColor(Color.RED).build();

    }

}
